package com.lul15834.orderfood.dao;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.lul15834.orderfood.bean.OrderBean;
import com.lul15834.orderfood.bean.OrderDetailBean;
import com.lul15834.orderfood.db.DBUntil;
import com.lul15834.orderfood.until.Tools;

import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    public static SQLiteDatabase db= DBUntil.con;
    //查看所有未完成的订单
    public static List<OrderDetailBean> getAllOrderDetail(String id){
        String sql="select * from d_order_details where s_details_id=?";
        Cursor rs=db.rawQuery(sql,new String[]{id});
        List<OrderDetailBean> orderDetailBeanList=new ArrayList<>();
        while (rs.moveToNext()){
            String detailId=Tools.getResultString(rs,"s_details_id");
            String foodId=Tools.getResultString(rs,"s_food_id");
            String foodName=Tools.getResultString(rs,"s_food_name");
            String foodDescription=Tools.getResultString(rs,"s_food_des");
            String foodPrice=Tools.getResultString(rs,"s_food_price");
            String foodQuantity=Tools.getResultString(rs,"s_food_num");
            String foodImg=Tools.getResultString(rs,"s_food_img");
            OrderDetailBean orderDetailBean=new OrderDetailBean(detailId,foodId,foodName,foodDescription,foodPrice,foodQuantity,foodImg);
            orderDetailBeanList.add(orderDetailBean);


        }
        return orderDetailBeanList;
    }

    /**
     * 实现更改订单图
     * @param orderId
     * @param newSta
     * @return
     */
    public static int updateOrderSta(String orderId,String newSta){
        String sql="update d_orders set s_order_sta=? where s_order_id=?";
        try {
            db.rawQuery(sql,new String[]{newSta,orderId});
            return 1;
        }catch (SQLException e){
            return 0;
        }
    }

    /**
     * 实现添加订单
     * @param orderId
     * @param time
     * @param businessId
     * @param userId
     * @param orderDetailsId
     * @param orderSta
     * @param orderAddress
     * @return
     */
    public static int insertOrderSta(String orderId,String time,String businessId,String userId,String orderDetailsId,String orderSta,String orderAddress){
        try {
            db.execSQL("INSERT INTO d_orders (s_order_id, s_order_time, s_business_id, s_user_id,s_order_details_id,s_order_sta, s_order_address) " +
                            "VALUES (?, ?, ?, ?, ?, ?,?)",
                    new Object[]{orderId, time,businessId ,userId,
                            orderDetailsId,orderSta,orderAddress});
            return 1;
        }catch (SQLException e){
            return 0;
        }
    }
    //查寻所有订单
    public static List<OrderBean> getAllOrder(){
        String sql="select * from d_orders  order by " +
                "strftime('%Y-%m-%d %H:%M:%S',s_order_time) desc ";
        Cursor rs=db.rawQuery(sql,null);
        List<OrderBean> orderDetailBeanList=new ArrayList<>();
        while (rs.moveToNext()){
            String orderId=Tools.getResultString(rs,"s_order_id");
            String orderTime=Tools.getResultString(rs,"s_order_time");
            String businessId=Tools.getResultString(rs,"s_business_id");
            String userId=Tools.getResultString(rs,"s_user_id");
            String orderDesId=Tools.getResultString(rs,"s_order_details_id");
            String orderSta=Tools.getResultString(rs,"s_order_sta");
            String orderAddress=Tools.getResultString(rs,"s_order_address");
            OrderBean orderBean=new OrderBean(orderId,orderTime,businessId,userId,orderDesId,orderSta,orderAddress);
            orderDetailBeanList.add(orderBean);


        }
        return orderDetailBeanList;
    }
    //根据订单状态查寻订单
    public static List<OrderBean> getAllOrderBySta(String account,String sta){
        String data[]={account,sta};
        String sql="select * from d_orders where s_business_id=? and s_order_sta=? order by " +
                "strftime('%Y-%m-%d %H:%M:%S',s_order_time) desc ";
        Cursor rs=db.rawQuery(sql,data);
        List<OrderBean> orderDetailBeanList=new ArrayList<>();
        while (rs.moveToNext()){
            String orderId=Tools.getResultString(rs,"s_order_id");
            String orderTime=Tools.getResultString(rs,"s_order_time");
            String businessId=Tools.getResultString(rs,"s_business_id");
            String userId=Tools.getResultString(rs,"s_user_id");
            String orderDesId=Tools.getResultString(rs,"s_order_details_id");
            String orderSta=Tools.getResultString(rs,"s_order_sta");
            String orderAddress=Tools.getResultString(rs,"s_order_address");
            OrderBean orderBean=new OrderBean(orderId,orderTime,businessId,userId,orderDesId,orderSta,orderAddress);
            orderDetailBeanList.add(orderBean);


        }
        return orderDetailBeanList;
    }
    //根据订单状态查寻订单
    public static List<OrderBean> getAllOrderByStaAndUser(String account,String sta){
        String data[]={account,sta};
        String sql="select * from d_orders where s_user_id=? and s_order_sta=? order by " +
                "strftime('%Y-%m-%d %H:%M:%S',s_order_time) desc ";
        Cursor rs=db.rawQuery(sql,data);
        List<OrderBean> orderDetailBeanList=new ArrayList<>();
        while (rs.moveToNext()){
            String orderId=Tools.getResultString(rs,"s_order_id");
            String orderTime=Tools.getResultString(rs,"s_order_time");
            String businessId=Tools.getResultString(rs,"s_business_id");
            String userId=Tools.getResultString(rs,"s_user_id");
            String orderDesId=Tools.getResultString(rs,"s_order_details_id");
            String orderSta=Tools.getResultString(rs,"s_order_sta");
            String orderAddress=Tools.getResultString(rs,"s_order_address");
            OrderBean orderBean=new OrderBean(orderId,orderTime,businessId,userId,orderDesId,orderSta,orderAddress);
            orderDetailBeanList.add(orderBean);
        }
        return orderDetailBeanList;
    }
    //根据订单状态查寻订单
    public static List<OrderBean> getAllOrderByStaAndUserFinish(String account,String sta){
        String data[]={account,sta};
        String sql="select * from d_orders where s_user_id=? and s_order_sta!=? order by " +
                "strftime('%Y-%m-%d %H:%M:%S',s_order_time) desc ";
        Cursor rs=db.rawQuery(sql,data);
        List<OrderBean> orderDetailBeanList=new ArrayList<>();
        while (rs.moveToNext()){
            String orderId=Tools.getResultString(rs,"s_order_id");
            String orderTime=Tools.getResultString(rs,"s_order_time");
            String businessId=Tools.getResultString(rs,"s_business_id");
            String userId=Tools.getResultString(rs,"s_user_id");
            String orderDesId=Tools.getResultString(rs,"s_order_details_id");
            String orderSta=Tools.getResultString(rs,"s_order_sta");
            String orderAddress=Tools.getResultString(rs,"s_order_address");
            OrderBean orderBean=new OrderBean(orderId,orderTime,businessId,userId,orderDesId,orderSta,orderAddress);
            orderDetailBeanList.add(orderBean);
        }
        return orderDetailBeanList;
    }
    //根据订单状态查寻订单
    public static List<OrderBean> getAllOrderFinish(String account){
        String data[]={account,"1"};
        String sql="select * from d_orders where s_business_id=? and s_order_sta!=? order by " +
                "strftime('%Y-%m-%d %H:%M:%S',s_order_time) desc ";
        Cursor rs=db.rawQuery(sql,data);
        List<OrderBean> orderDetailBeanList=new ArrayList<>();
        while (rs.moveToNext()){
            String orderId=Tools.getResultString(rs,"s_order_id");
            String orderTime=Tools.getResultString(rs,"s_order_time");
            String businessId=Tools.getResultString(rs,"s_business_id");
            String userId=Tools.getResultString(rs,"s_user_id");
            String orderDesId=Tools.getResultString(rs,"s_order_details_id");
            String orderSta=Tools.getResultString(rs,"s_order_sta");
            String orderAddress=Tools.getResultString(rs,"s_order_address");
            OrderBean orderBean=new OrderBean(orderId,orderTime,businessId,userId,orderDesId,orderSta,orderAddress);
            orderDetailBeanList.add(orderBean);


        }
        return orderDetailBeanList;
    }

    /**
     *
     * @param orderDetailBean
     * @return
     */
    public static void saveOrderDetail(OrderDetailBean orderDetailBean){
        db.execSQL("INSERT INTO d_order_details (s_details_id,s_food_id, s_food_name, s_food_des, s_food_price, s_food_num, s_food_img) " +
                        "VALUES (?, ?, ?, ?, ?, ?,?)",
                new Object[]{orderDetailBean.getDetailId(),orderDetailBean.getFoodId(),
                        orderDetailBean.getFoodName(), orderDetailBean.getFoodDescription(),
                        orderDetailBean.getFoodPrice(),orderDetailBean.getFoodQuantity(),
                        orderDetailBean.getFoodImg()});
    }

}
